import sys, os, wx, time, re
import version
import cards, deck, deckbuilder

class Engine():

    def __init__(self):
        self.Application = wx.App(redirect=True,filename="ws.log")
        self.BaseDirectory = os.getcwd()
        self.SkinsDirectory = os.path.join(self.BaseDirectory, 'skins')
        self.ImagesDirectory = os.path.join(self.BaseDirectory, 'images')

        self.CardDict = cards.Cards()
        self.Deck = deck.Deck()

        self.Frame = deckbuilder.MainFrame(engine=self, parent=None, title="Weib Schwarz Deck Builder",
                                           size=(1300, 800))

        self.Frame.Show()
        self.Application.MainLoop()

    def GetNameVersion(self):
        return "%s %s" % (version.GetName(), version.GetVersion())

    def FindCardByNameLike(self, name):
        li = list()
        for card in self.CardDict.GetSortedValues():
            if name in card.Name:
                li.append(card)
        return li

    def GetImageSkin(self, name):
        path = os.path.join(self.SkinsDirectory, name + '.png')
        if os.path.exists(path):
            return wx.Bitmap(path)
        return -1

    def GetImageCard(self, name):
        path = os.path.join(self.ImagesDirectory, name + '.jpg')
        if os.path.exists(path):
            return wx.Bitmap(path)
        return -1

    def GetImageCardScaled(self, name):
        path = os.path.join(self.ImagesDirectory, name + '.jpg')
        if os.path.exists(path):
            image = wx.Image(path)
            image.Rescale(289, 420, wx.IMAGE_QUALITY_HIGH)
            return wx.BitmapFromImage(image)
        return -1

    def GetImageBigCard(self, card):
        dc = wx.MemoryDC()
        dc.SelectObject(self.GetImageSkin('CardBack'))
        cbmp = self.GetImageCardScaled(card.Image)
        if not cbmp is -1:
            dc.DrawBitmap(cbmp, 0, 0)
        return dc.GetAsBitmap()

if __name__ == "__main__":
    e = Engine()
